import 'package:word_game/engine/core.dart';
import 'MergeThing.dart';

class MergeThingEvent extends Eventable {
  String mergeThingCode;
  MergeThingEvent(String code) : super('合成') {
    this.mergeThingCode = code;
  }
  
  @override
  void doEvent(owner) {
    var mt = newMergeThing(mergeThingCode);
    World.instance.hero.getThing(mt);
  }

  MergeThing newMergeThing(String code) {
    MergeThing mt = MaterialManager.instance.newMaterialWithCode(code) as MergeThing;
    bool hasAllSource = true;
    for (var code in mt.sourceThingCodes) {
      var thing = World.instance.hero.bag.things.firstWhere((test)=>test.code==code);
      if (thing==null) {
        hasAllSource = false;
        break;
      }
    }
    if (!hasAllSource) {
      Engine.instance.displayer.displayString('缺少足够的材料');
    }
    for (var code in mt.sourceThingCodes) {
      var thing = World.instance.hero.bag.things.firstWhere((test)=>test.code==code);
      World.instance.hero.pureRemoveThing(thing);
    }
    return mt;
  }
}